public class Demo6 {
    public static boolean isQuit = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!isQuit){
                System.out.println("线程正在运行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();
        //sleep模拟主线程中要执行的逻辑
        Thread.sleep(3000);
        //此时想要终止t线程,就把标志位改为true,使t线程被终止
        isQuit = true;
        System.out.println("t线程被终止");
    }
}
